/* strerror_test.c

   A program to test whether the implementation of strerror() thread-safe.
*/
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* Get declaration of strerror() */

void errExitEN(int errn, char *msg) {
  fprintf(stderr, "%s: %s\n", msg, strerror(errn));
  exit(EXIT_FAILURE);
}

static void *threadFunc(void *arg) {
  char *str;

  printf("Other thread about to call strerror()\n");
  str = strerror(EPERM);
  printf("Other thread: str (%p) = %s\n", str, str);

  return NULL;
}

int main(int argc, char *argv[]) {
  pthread_t t;
  int s;
  char *str;

  str = strerror(EINVAL);
  printf("Main thread has called strerror()\n");

  s = pthread_create(&t, NULL, threadFunc, NULL);
  if (s != 0) errExitEN(s, "pthread_create");

  s = pthread_join(t, NULL);
  if (s != 0) errExitEN(s, "pthread_join");

  /* If strerror() is not thread-safe, then the output of this printf() be
     the same as that produced by the analogous printf() in threadFunc() */

  printf("Main thread:  str (%p) = %s\n", str, str);

  exit(EXIT_SUCCESS);
}